package com.example.pucha_xian.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.example.pucha_xian.R;
import com.example.pucha_xian.common.PointDTO;


import java.util.List;

public class ArcgisMapAdapter extends RecyclerView.Adapter<ArcgisMapAdapter.ViewHolder> {
    private List<PointDTO> pointDTOS;
    private RecyclerView recyclerView;
    private MapView mapView;

    public ArcgisMapAdapter(List<PointDTO> pointDTOS, RecyclerView recyclerView, MapView mapView) {
        this.pointDTOS = pointDTOS;
        this.recyclerView = recyclerView;
        this.mapView=mapView;
    }

    @NonNull
    @Override
    public ArcgisMapAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.super_recycleview,parent,false);
        ArcgisMapAdapter.ViewHolder viewHolder = new ArcgisMapAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ArcgisMapAdapter.ViewHolder holder, int position) {
        PointDTO pointDTO=pointDTOS.get(position);
        holder.tv_pointId.setText(pointDTO.getPointId());
        holder.tv_name.setText(pointDTO.getName());
        holder.tv_status.setText(pointDTO.getStatus());
        holder.iv_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Point point2D=new Point(pointDTO.getLng(),pointDTO.getLat(), SpatialReference.create(4490));
                mapView.setViewpointCenterAsync(point2D,20000);
                recyclerView.setVisibility(View.INVISIBLE);

            }
        });

    }

    @Override
    public int getItemCount() {
        return pointDTOS.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        private TextView tv_pointId;
        private TextView tv_name;
        private TextView tv_status;
        private ImageView iv_location;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_pointId=itemView.findViewById(R.id.tv_pointID_item);
            tv_name=itemView.findViewById(R.id.tv_name_item);
            tv_status=itemView.findViewById(R.id.tv_status_item);
            iv_location=itemView.findViewById(R.id.iv_location_item);
        }
    }
}
